package com.dhu.wx.handler;

import com.dhu.wx.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author gongjiawei
 * @Date 2025/3/5 12:25
 * @注释
 */
@Slf4j
@Component
public class ReceiveTextMsgHandler implements WxChatMsgHandler{
    private static final String KEY_WORD="验证码";

    private static final String LOGIN_PREFIX="loginCode";


    @Resource
    private RedisUtil redisUtil;

    @Override
    public WxChatMsgTypeEnum getMsgType() {
        return WxChatMsgTypeEnum.TEXT_MSG;
    }

    @Override
    public String dealMsg(Map<String, String> messageMap) {
        log.info("触发接收消息");
        String content = messageMap.get("Content");
        if (!KEY_WORD.equals(content)){
            return "";
        }
        Random random=new Random();
        int num = random.nextInt(10000);

        String numContent="您当前的验证码是："+num+"  有限时间为5分钟";
        String fromUserName = messageMap.get("FromUserName");
        String toUserName = messageMap.get("ToUserName");


        String msgType = messageMap.get("MsgType");
        String createTimeStr = messageMap.get("CreateTime");
        Long createTime = null;
        if (createTimeStr != null && !createTimeStr.isEmpty()) {
            try {
                createTime = Long.valueOf(createTimeStr);
            } catch (NumberFormatException e) {
                log.error("Invalid CreateTime value: {}", createTimeStr, e);
                // 可以选择使用当前时间作为默认值
                createTime = System.currentTimeMillis() / 1000;
            }
        } else {
            log.warn("CreateTime is null or empty, using current time.");
            createTime = System.currentTimeMillis() / 1000;
        }
        String msgIdStr = messageMap.get("MsgId");
        Long msgId = null;
        if (msgIdStr != null && !msgIdStr.isEmpty()) {
            try {
                msgId = Long.valueOf(msgIdStr);
            } catch (NumberFormatException e) {
                log.error("Invalid MsgId value: {}", msgIdStr, e);
                // 可以选择使用默认值或其他处理方式
                msgId = System.currentTimeMillis();
            }
        } else {
            log.warn("MsgId is null or empty, using current time.");
            msgId = System.currentTimeMillis();
        }
        //存入redis
        String buildKey = redisUtil.buildKey(LOGIN_PREFIX,String.valueOf(num));
        redisUtil.setNx(buildKey,fromUserName,5L, TimeUnit.MINUTES);


        return "<xml>\n" +
                "  <ToUserName><![CDATA["+fromUserName+"]]>" + "</ToUserName>\n" +
                "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
                "  <CreateTime>"+createTime+"</CreateTime>\n" +
                "  <MsgType><![CDATA["+msgType+"]]></MsgType>\n" +
                "  <Content><![CDATA["+numContent+"]]></Content>\n" +
                "  <MsgId><![CDATA["+msgId+"]]></MsgId>\n "+
                "</xml>";
    }
}
